package com.estrend.redbird.base

import NetworkStatusMonitor
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.lifecycleScope
import com.king.frame.mvvmframe.base.BaseActivity
import com.king.frame.mvvmframe.base.BaseViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch


abstract class BaseRedBirdActivity<VM : BaseViewModel, VDB : ViewDataBinding>:BaseActivity<VM,VDB>() {

    private var  networkStatusMonitor:NetworkStatusMonitor?=null

    private var netJob:Job? = null;

    private var enableNetworkStatusMonitor = true;
    abstract fun reFreshOnline()

    fun showNoNetWork(){

    }

    override fun attachBaseContext(newBase: Context) {
        val newOverride = newBase.resources.configuration
        newOverride.fontScale = 1.0f  // Override font scale to 1.0 (default size)
        val context = newBase.createConfigurationContext(newOverride)
        super.attachBaseContext(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 设置沉浸式状态栏
        var uiOptions: Int = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.getDecorView().setSystemUiVisibility(uiOptions)
        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

    }


    override fun onResume() {
        super.onResume()
        if (enableNetworkStatusMonitor){
            networkStatusMonitor = NetworkStatusMonitor(this);
            lifecycleScope.launch {
                netJob =  networkStatusMonitor!!.networkStatus.collect{
                    if (it){//Network Available
                        reFreshOnline()
                    }else{//Network NotAvailable
                        showNoNetWork()
                    }
                }
            }
        }

    }

    override fun onPause() {
        super.onPause()
        if (networkStatusMonitor!=null){
            networkStatusMonitor!!.stopNoNetworkCheck();
            netJob?.cancel()
        }

    }
}